﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XMK.Framework.Model;
using XMK.Tadong.Data;
using XMK.Tadong.Model;
using XMK.Utils;
using XMK.Tadong.Model.Cond;
using System.Data;
using System.Collections;
using System.Web.SessionState;
namespace XMK.Tadong.Biz
{
  public  class Tb_TeamBiz
    {
      public Tb_TeamBiz()
      { 
      }
      /// <summary>
      /// 获的课题组对象
      /// </summary>
      /// <param name="teamID"></param>
      /// <returns></returns>
      public TB_TEAMInfo GetTeamInfoById(decimal teamID)
      {
          //Base_RM_UserInfoBiz userBiz = new Base_RM_UserInfoBiz();   
          //List<Base_UserInfo> userList =(List<Base_UserInfo>)userBiz.QueryUserInfoList(null).Data;
          //Hashtable htUser = new Hashtable(); //创建一个Hashtable实例
          //foreach (Base_UserInfo userInfo in userList)
          //{
          //    htUser.Add(userInfo.USER_ID, userInfo.USER_NAME);
          //}

          //Base_UserInfo single = from n in userList where n.USER_ID ==  select n;

          Tb_TeamData data = new Tb_TeamData(Config.TadongDbProvider);
         // TB_TEAMInfo teamInfo = data.GetTeamInfoById(Convert.ToString(teamID));

          //if (htUser.Contains(teamInfo.CREATE_USER))
          //{
          //    teamInfo.CREATE_USER = htUser[teamInfo.CREATE_USER].ToString();
          //}
          //if (htUser.Contains(teamInfo.PRO_LEADER))
          //{
          //    teamInfo.PRO_LEADER = htUser[teamInfo.PRO_LEADER].ToString();
          //}
          //if (htUser.Contains(teamInfo.RES_MANAGE))
          //{
          //    teamInfo.RES_MANAGE = htUser[teamInfo.RES_MANAGE].ToString();
          //}

          return data.GetTeamInfoById(Convert.ToString(teamID));
      }

      /// <summary>
      /// 删除课题组
      /// </summary>
      /// <param name="teamId"></param>
      /// <returns></returns>
      public bool DeleteTeamInfo(string teamId)
      {
          //bool result =
          Tb_TeamData data = new Tb_TeamData(Config.TadongDbProvider);
          return data.DeleteTeamInfo(teamId);
      }

      /// <summary>
      /// 获的课题组对象
      /// </summary>
      /// <param name="teamID"></param>
      /// <returns></returns>
      public ResponseInfo QueryTeamInfoById(string teamID)
      {
          ResponseInfo response = new ResponseInfo();
          Tb_TeamData data = new Tb_TeamData(Config.TadongDbProvider);
          response.Data= (TB_TEAMInfo)data.GetTeamInfoById(teamID);
          return response;
      }

      /// <summary>
      /// 根据课题组名获得课题组Id
      /// </summary>
      /// <param name="teamName"></param>
      /// <returns></returns>
      public string GetTeamIdbyName(string teamName)
      {
          Tb_TeamData data = new Tb_TeamData(Config.TadongDbProvider);
          return data.GetTeamIdbyName(teamName);
      }
      /// <summary>
      /// 添加课题组
      /// </summary>
      /// <param name="teamObj"></param>
      /// <returns></returns>
      public bool addTeam(TB_TEAMInfo teamObj)
      {
          try
          {
              Tb_TeamData data = new Tb_TeamData(Config.TadongDbProvider);
              teamObj.TEAM_ID = data.getMaxTeamId() + 1;
              teamObj.CREATE_TIME = DateTime.Now;
              teamObj.UPD_TIME = DateTime.Now;
              if (teamObj.START_DATE != null)
              {
                  DateTime dt =(DateTime) teamObj.START_DATE;
                  dt= dt.AddHours(1).AddMinutes(1).AddSeconds(1);
                  teamObj.START_DATE = dt;
              }
              if (teamObj.END_DATE != null)
              {
                  DateTime dt = (DateTime)teamObj.END_DATE;
                  dt = dt.AddHours(1).AddMinutes(1).AddSeconds(1);
                  teamObj.END_DATE = dt;
              }

              bool config= data.InsertNewTeam(teamObj);
              if (config)
              { 
                  //将team_leader插入到team_user表
                  Base_RM_UserInfoBiz userBiz = new Base_RM_UserInfoBiz();
                  return userBiz.AddTeamUser(teamObj.TEAM_ID,teamObj.TEAM_LEADER);
              }
          }
          catch (Exception ex)
          {
              string str = ex.Message;
          }
          return false;
      }


      /// <summary>
      /// 修改课题组
      /// </summary>
      /// <param name="teamObj"></param>
      /// <returns></returns>
      public bool UpdateTeam(TB_TEAMInfo teamObj)
      {
          try
          {
              Tb_TeamData data = new Tb_TeamData(Config.TadongDbProvider);
              teamObj.UPD_TIME = DateTime.Now;
              bool config= data.UpdateTeamInfo(teamObj);
              if (config)
              {
                  //将team_leader插入到team_user表
                  Base_RM_UserInfoBiz userBiz = new Base_RM_UserInfoBiz();
                  return userBiz.AddTeamUser(teamObj.TEAM_ID, teamObj.TEAM_LEADER);
              }

          }
          catch (Exception ex)
          {
              string str = ex.Message;
          }
          return false;
      }

      public DataTable dtQueryCstInfo(string type)
      {
          Tb_TeamData data = new Tb_TeamData(Config.TadongDbProvider);
          return data.QueryCstInfo(type);

      }

      //绑定密级
      public DataTable GetMJ(string Type)
      {
          Tb_TeamData data = new Tb_TeamData(Config.TadongDbProvider);
          return data.GetMJ(Type);
      }


      /// <summary>
        /// 通过userid获取当前用户所在的课题组
        /// </summary>
        /// <returns>文件列表</returns>
      public DataTable GetUTeamList(string User_ID)
      {
          Tb_TeamData data = new Tb_TeamData(Config.TadongDbProvider);
          return data.GetTeamListBySql(User_ID);
      }


      public ResponseInfo QueryCstInfo(string type)
      {
          ResponseInfo response = null;
          try
          {
              response = new ResponseInfo();
              Tb_TeamData data = new Tb_TeamData(Config.TadongDbProvider);
              response.Data = data.QueryCstInfo(type);
          }
          catch (Exception ex)
          {
              response = new ResponseInfo("获取字典信息失败！");
              LogHelper.WriteLog(LogType.Error, "[QueryCstInfo:]" + ex.ToString());
          }

          return response;
      }

      public ResponseInfo QueryTeamList_()
      {
          ResponseInfo response = null;
          try
          {
              response = new ResponseInfo();
              Tb_TeamData data = new Tb_TeamData(Config.TadongDbProvider);
              response.Data = (List<TB_TEAMInfo>)data.GetTeamInfoList();

          }
          catch (Exception ex)
          {
              response = new ResponseInfo("获取课题组信息失败！");
              LogHelper.WriteLog(LogType.Error, "[QueryTeamList_:]" + ex.ToString());
          }

          return response;
      }

      /// <summary>
      /// 课题组信息列表
      /// </summary>
      /// <param name="cond"></param>
      /// <param name="page"></param>
      /// <param name="order"></param>
      /// <returns></returns>
      public ResponseInfo QueryTeamList(PageInfo page, OrderInfo order)
      {
          ResponseInfo response = null;
          try
          {
              response = new ResponseInfo();

              Base_RM_UserInfoBiz userBiz = new Base_RM_UserInfoBiz();
              List<Base_UserInfo> userList = (List<Base_UserInfo>)userBiz.QueryUserInfoList(null).Data;
              Hashtable htUser = new Hashtable(); //创建一个Hashtable实例
              foreach (Base_UserInfo userInfo in userList)
              {
                  htUser.Add(userInfo.USER_ID, userInfo.USER_NAME);
              }

          
              Tb_TeamData data = new Tb_TeamData(Config.TadongDbProvider);
              List<TB_TEAMInfo> teamInfoList = (List<TB_TEAMInfo>)data.GetTeamInfoList();

              DataTable dt_xmlx = data.QueryCstInfo("2");//项目类型
              DataTable dt_xmjb = data.QueryCstInfo("3");//项目级别
              DataTable dt_zylx = data.QueryCstInfo("4");//专业类型
              for ( int i=0;i<teamInfoList.Count;i++ )
              {
                  TB_TEAMInfo teamInfo = teamInfoList[i];
                  if (htUser.Contains(teamInfo.CREATE_USER))
                  {
                      teamInfo.CREATE_USER = htUser[teamInfo.CREATE_USER].ToString();
                  }
                  if (htUser.Contains(teamInfo.PRO_LEADER)) 
                  {
                      teamInfo.PRO_LEADER = htUser[teamInfo.PRO_LEADER].ToString();
                  }
                  if (htUser.Contains(teamInfo.RES_MANAGE))
                  {
                      teamInfo.RES_MANAGE = htUser[teamInfo.RES_MANAGE].ToString();
                  }
                  if (htUser.Contains(teamInfo.TEAM_LEADER))
                  {
                      teamInfo.TEAM_LEADER = htUser[teamInfo.TEAM_LEADER].ToString();
                  }
                  if (teamInfo.OUTHELP == "1")
                  {
                      teamInfo.OUTHELP = "外协";
                  }
                  else if (teamInfo.OUTHELP == "0")
                  {
                      teamInfo.OUTHELP = "非外协";
                  }

                  if ( teamInfo.PRO_LEVEL!=null)
                  {
                      foreach (DataRow dr in dt_xmjb.Rows)
                      {
                          if (Convert.ToDecimal( dr["CST_NUM"]) == teamInfo.PRO_LEVEL)
                          {
                              teamInfo.PRO_LEVEL_STR = dr["CST_DESC"].ToString();
                          }
                      }
                  }
                  if (teamInfo.PRO_TYPE != null)//项目类型
                  {
                      foreach (DataRow dr in dt_xmlx.Rows)
                      {
                          if (Convert.ToDecimal(dr["CST_NUM"]) == teamInfo.PRO_TYPE)
                          {
                              teamInfo.PRO_TYPE_STR = dr["CST_DESC"].ToString();
                          }
                      }
                  }
                  if (teamInfo.PROFE_TYPE != null)//专业类型
                  {
                      foreach (DataRow dr in dt_zylx.Rows)
                      {
                          if (Convert.ToDecimal(dr["CST_NUM"]) == teamInfo.PROFE_TYPE)
                          {
                              teamInfo.PROFE_TYPE_STR = dr["CST_DESC"].ToString();
                          }
                      }

                  }
                  //承建单位
                  if (teamInfo.ROLES_ID.ToString() != "")
                  {
                      Base_OrganizationBiz orgBiz = new Base_OrganizationBiz();
                      Base_OrganizationInfo orgInfo = orgBiz.getOrgInfoById(teamInfo.ROLES_ID.ToString());
                      teamInfo.ROLES_ID = orgInfo.ORGANIZATION_NAME.ToString();
                  }
                  string startTime=(Convert.ToDateTime(teamInfo.START_DATE.ToString())).ToShortDateString();
                  teamInfo.START_DATE = DateTime.Parse(startTime);

                  teamInfoList[i] = teamInfo;
              }


              response.Data = teamInfoList;
              response.Page = page;


           
          }
          catch (Exception ex)
          {
              response = new ResponseInfo("获取课题组信息失败！");
              LogHelper.WriteLog(LogType.Error, "[QueryTeamList:]" + ex.ToString());
          }

          return response;
      }


      /// <summary>
      /// 课题组组长所管理的课题组信息列表
      /// </summary>
      /// <param name="cond"></param>
      /// <param name="page"></param>
      /// <param name="order"></param>
      /// <returns></returns>
      public ResponseInfo QueryMyManaTeamList(PageInfo page, OrderInfo order)
      {
          ResponseInfo response = null;
          string USER_ID = LoginUserBiz.LoginUserID();
          try
          {
              response = new ResponseInfo();

              Base_RM_UserInfoBiz userBiz = new Base_RM_UserInfoBiz();
              List<Base_UserInfo> userList = (List<Base_UserInfo>)userBiz.QueryUserInfoList(null).Data;
              Hashtable htUser = new Hashtable(); //创建一个Hashtable实例
              foreach (Base_UserInfo userInfo in userList)
              {
                  htUser.Add(userInfo.USER_ID, userInfo.USER_NAME);
              }


              Tb_TeamData data = new Tb_TeamData(Config.TadongDbProvider);
              List<TB_TEAMInfo> teamInfoList = (List<TB_TEAMInfo>)data.QueryMyManaTeamList(USER_ID);

              DataTable dt_xmlx = data.QueryCstInfo("2");//项目类型
              DataTable dt_xmjb = data.QueryCstInfo("3");//项目级别
              DataTable dt_zylx = data.QueryCstInfo("4");//专业类型
              for (int i = 0; i < teamInfoList.Count; i++)
              {
                  TB_TEAMInfo teamInfo = teamInfoList[i];
                  if (htUser.Contains(teamInfo.CREATE_USER))
                  {
                      teamInfo.CREATE_USER = htUser[teamInfo.CREATE_USER].ToString();
                  }
                  if (htUser.Contains(teamInfo.PRO_LEADER))
                  {
                      teamInfo.PRO_LEADER = htUser[teamInfo.PRO_LEADER].ToString();
                  }
                  if (htUser.Contains(teamInfo.RES_MANAGE))
                  {
                      teamInfo.RES_MANAGE = htUser[teamInfo.RES_MANAGE].ToString();
                  }
                  if (htUser.Contains(teamInfo.TEAM_LEADER))
                  {
                      teamInfo.TEAM_LEADER = htUser[teamInfo.TEAM_LEADER].ToString();
                  }
                  if (teamInfo.OUTHELP == "1")
                  {
                      teamInfo.OUTHELP = "外协";
                  }
                  else if (teamInfo.OUTHELP == "0")
                  {
                      teamInfo.OUTHELP = "非外协";
                  }

                  if (teamInfo.PRO_LEVEL != null)
                  {
                      foreach (DataRow dr in dt_xmjb.Rows)
                      {
                          if (Convert.ToDecimal(dr["CST_NUM"]) == teamInfo.PRO_LEVEL)
                          {
                              teamInfo.PRO_LEVEL_STR = dr["CST_DESC"].ToString();
                          }
                      }
                  }
                  if (teamInfo.PRO_TYPE != null)//项目类型
                  {
                      foreach (DataRow dr in dt_xmlx.Rows)
                      {
                          if (Convert.ToDecimal(dr["CST_NUM"]) == teamInfo.PRO_TYPE)
                          {
                              teamInfo.PRO_TYPE_STR = dr["CST_DESC"].ToString();
                          }
                      }
                  }
                  if (teamInfo.PROFE_TYPE != null)//专业类型
                  {
                      foreach (DataRow dr in dt_zylx.Rows)
                      {
                          if (Convert.ToDecimal(dr["CST_NUM"]) == teamInfo.PROFE_TYPE)
                          {
                              teamInfo.PROFE_TYPE_STR = dr["CST_DESC"].ToString();
                          }
                      }

                  }
                  //承建单位
                  if (teamInfo.ROLES_ID.ToString() != "")
                  {
                      Base_OrganizationBiz orgBiz = new Base_OrganizationBiz();
                      Base_OrganizationInfo orgInfo = orgBiz.getOrgInfoById(teamInfo.ROLES_ID.ToString());
                      teamInfo.ROLES_ID = orgInfo.ORGANIZATION_NAME.ToString();
                  }
                  string startTime = (Convert.ToDateTime(teamInfo.START_DATE.ToString())).ToShortDateString();
                  teamInfo.START_DATE = DateTime.Parse(startTime);

                  teamInfoList[i] = teamInfo;
              }


              response.Data = teamInfoList;
              response.Page = page;



          }
          catch (Exception ex)
          {
              response = new ResponseInfo("获取课题组信息失败！");
              LogHelper.WriteLog(LogType.Error, "[QueryTeamList:]" + ex.ToString());
          }

          return response;
      }


    }
}
